

$(document).ready(function(){
	$(".tagsinput span.badge.tag.action i").each(function(i,e){
		$(e).click(function(event){
			var id=$(e).attr("data");
			removeUserTag(id,e);		
		});
	});
	
	var timeout=null;
	$(".tagsinput-add-container.badge input").each(function(i,e){
		$(e).focus(function(event){
			if(timeout!==null){
				clearTimeout(timeout);
			}
			$(e).animate({width:"100px"},200,function(){});
		});
		
		$(e).blur(function(event){
			timeout=setTimeout(function(){$(e).animate({width:"40px"},200,function(){});},5000);
		});
		$(e).keydown(function(event){
			if(event.keyCode==13){
	        	 $(e).parent().find("span").eq(0).click();
	         }
		});
	});
	
});

function removeUserTag(tagid,e){
	var data={id:tagid,type:"user"};
	$.ajax({    
        type: "POST",  
        url: "user/remove-tag.json",
        contentType : 'application/json',
        data:JSON.stringify(data),
        dataType:'json',
        success: function(response){ 
        	if(response.ok){
        		$(e).parent().hide();
        	}else{
        		if(response.msg==="请先登录"){
        			triggerLogin();
        		}
        	}
        },
	 	error: function(e){    }
	});	
}
	function buildArticle(map){
		if(map.user_head===null)
			user_head="default.jpg";
		else
			user_head=map.user_head;
		var html = '';
		html+="<div class='article event'>";
		html+="	<div class='head'>";
		html+="		<a href='c/home/"+map.user_id+".html'><img src='upload/head/"+user_head+"?s=50' onerror='this.src=\"upload/head/default.jpg?s=50\"'/></a>";
		html+="	</div>";
		html+=" <div class='body'>";
		html+="	<div class='hd'>";
		html+="		<p>";
		html+="			<span class='name'><a href='c/home/"+map.user_id+".html'>"+map.user_name+"</a></span>";
		html+="			<span>发表短文：</span>";
		html+="        "+map.title;
		html+="		</p>";
		html+="	</div>";
		html+="	<div class='bd'>";
		html+="		<p class='show'>";
		html+="		"+map.content;
		html+="		</p>";
		html+="	</div>";
		html+="	<div class='ft'>";
		html+="		<p>";
		html+="			<span class='date'>";
		html+="			"+buildDate(map.create_at);
		html+="			</span>";
		html+="			<span class='action up' onclick=\"doAction('article','agree',"+map.id+",this)\">";
		html+="				顶(<em>"+map.agree_num+"</em>)";
		html+="			</span>";
		html+="			<span class='action down' onclick=\"doAction('article','disagree',"+map.id+",this)\">";
		html+="				踩(<em>"+map.disagree_num+"</em>)";
		html+="			</span>";
		html+="			<span class='action' onclick=\"doAction('article','collect',"+map.id+",this)\">";
		html+="				收藏(<em>"+map.collect_num+"</em>)";
		html+="			</span>";
		html+="			<span class='action' onclick=\"doAction('article','favor',"+map.id+",this)\">";
		html+="				喜欢(<em>"+map.favor_num+"</em>)";
		html+="			</span>";
		html+="			<span class='action comment'>";
		html+="				<a href='c/a/"+map.id+".html'>评论(<em>"+map.comments_num+"</em>)</a>";
		html+="			</span>";
		html+="		</p>";
		html+="	</div>";
		html+="</div>";
		html+="</div>";
		return html;
	}
	function buildQuestion(map){
		if(map.user_head===null){
			user_head="default.jpg";
		}else{
			user_head=map.user_head;
		}
		if(map.user_by_head===null){
			user_by_head="default.jpg";
		}else{
			user_by_head=map.user_by_head;
		}
			
		var html = '';
		html+="<div class='question event'>";
		html+="	<div class='head'>";
		html+="		<a href='c/home/"+map.user_by_id+".html'><img src='upload/head/"+user_by_head+"?s=50' onerror='this.src=\"upload/head/default.jpg?s=50\"'/></a>";
		html+="		<a href='c/home/"+map.user_id+".html'><img src='upload/head/"+user_head+"?s=50' onerror='this.src=\"upload/head/default.jpg?s=50\"'/></a>";
		html+="		<span class='action'>问</span>";
		html+="	</div>";
		html+=" <div class='body'>";
		html+="	<div class='hd'>";
		html+="		<p>";
		html+="			<span class='name'><a href='c/home/"+map.user_by_id+".html'>"+map.user_by_name+"</a></span>";
		html+="			<span>&nbsp;问&nbsp;</span>";
		html+="			<span class='name'><a href='c/home/"+map.user_id+".html'>"+map.user_name+"</a>:</span>";
		html+="        "+map.question;
		html+="		</p>";
		html+="	</div>";
		html+="	<div class='bd'>";
		html+="	</div>";
		html+="	<div class='ft'>";
		html+="		<p>";
		html+="			<span class='date'>";
		html+="			"+buildDate(map.question_at);
		html+="			</span>";
		html+="			<span class='action up' onclick=\"doAction('question','agree',"+map.id+",this)\">";
		html+="				顶(<em>"+map.agree_num+"</em>)";
		html+="			</span>";
		html+="			<span class='action down' onclick=\"doAction('question','disagree',"+map.id+",this)\">";
		html+="				踩(<em>"+map.disagree_num+"</em>)";
		html+="			</span>";
		html+="			<span class='action' onclick=\"doAction('question','collect',"+map.id+",this)\">";
		html+="				收藏(<em>"+map.collect_num+"</em>)";
		html+="			</span>";
		html+="			<span class='action' onclick=\"doAction('question','favor',"+map.id+",this)\">";
		html+="				喜欢(<em>"+map.favor_num+"</em>)";
		html+="			</span>";
		html+="			<span class='action comment'>";
		html+="				<a href='c/q/"+map.id+".html'>评论(<em>"+map.comments_num+"</em>)</a>";
		html+="			</span>";
		html+="		</p>";
		html+="	</div>";
		html+="</div>";
		html+="</div>";
		return html;
	}
	function buildAnswer(map){
		if(map.user_head===null){
			user_head="default.jpg";
		}else{
			user_head=map.user_head;
		}
		if(map.user_by_head===null){
			user_by_head="default.jpg";
		}else{
			user_by_head=map.user_by_head;
		}
			
		var html = '';
		html+="<div class='question event'>";
		html+="	<div class='head'>";
		html+="		<a href='c/home/"+map.user_id+".html'><img src='upload/head/"+user_by_head+"?s=50' onerror='this.src=\"upload/head/default.jpg?s=50\"'/></a>";
		html+="		<a href='c/home/"+map.user_id+".html'><img src='upload/head/"+user_head+"?s=50' onerror='this.src=\"upload/head/default.jpg?s=50\"'/></a>";
		html+="		<span class='action'>答</span>";
		html+="	</div>";
		html+=" <div class='body'>";
		html+="	<div class='hd'>";
		html+="		<p>";
		html+="			<span class='name'><a href='c/home/"+map.user_id+".html'>"+map.user_name+"</a></span>";
		html+="			<span>&nbsp;答&nbsp;</span>";
		html+="			<span class='name'><a href='c/home/"+map.user_id+".html'>"+map.user_by_name+"</a>:</span>";
		html+="        "+map.question;
		html+="		</p>";
		html+="	</div>";
		html+="	<div class='bd'>";
		html+="		<p class='show'>";
		html+="		"+map.answer;
		html+="		</p>";
		html+="	</div>";
		html+="	<div class='ft'>";
		html+="		<p>";
		html+="			<span class='date'>";
		html+="			"+buildDate(map.question_at);
		html+="			</span>";
		html+="			<span class='action up' onclick=\"doAction('question','agree',"+map.id+",this)\">";
		html+="				顶(<em>"+map.agree_num+"</em>)";
		html+="			</span>";
		html+="			<span class='action down' onclick=\"doAction('question','disagree',"+map.id+",this)\">";
		html+="				踩(<em>"+map.disagree_num+"</em>)";
		html+="			</span>";
		html+="			<span class='action' onclick=\"doAction('question','collect',"+map.id+",this)\">";
		html+="				收藏(<em>"+map.collect_num+"</em>)";
		html+="			</span>";
		html+="			<span class='action' onclick=\"doAction('question','favor',"+map.id+",this)\">";
		html+="				喜欢(<em>"+map.favor_num+"</em>)";
		html+="			</span>";
		html+="			<span class='action comment'>";
		html+="				<a href='c/q/"+map.id+".html'>评论(<em>"+map.comments_num+"</em>)</a>";
		html+="			</span>";
		html+="		</p>";
		html+="	</div>";
		html+="</div>";
		html+="</div>";
		return html;
	}
	
	function buildComment(map,type){
		var html = '';
		html+="<div class='comment event'>";
		html+="<div class='head'>";
		html+="	<img src='upload/head/"+map.user_head+"?s=50' onerror='this.src=\"upload/head/default.jpg?s=50\"'/>";
		html+="</div>";
		html+="<div class='body'>";
		html+="<div class='hd'>";
		html+="	<p>";
		html+="		<a class='name' href=\"c/home/"+map.user_id+".html\">"+map.user_name+"</a>";
		html+="	</p>";
		html+="</div>";
		html+="<div class='bd'>";
		html+="	<p class='show'>";
		html+=	map.contents;
		html+="	</p>";
		html+="</div>";
		html+="<div class='ft'>";
		html+="	<p>";
		html+="		<span class='date'>";
		html+=			buildDate(map.create_at);
		html+="		</span>";
		html+="		<span class='action' onclick=\"doActionComment('"+type+"','agree',"+map.id+",this)\">";
		html+="			顶(<em>"+map.agree_num+"</em>)";
		html+="		</span>";
		html+="		<span class='action' onclick=\"doActionComment('"+type+"','disagree',"+map.id+",this)\">";
		html+="			踩(<em>"+map.disagree_num+"</em>)";
		html+="		</span>";
		html+="	</p>";
		html+="</div>";
		html+="</div>";
		html+="</div>";
		return html;
	}
	
	
	
	function doAction(type,action,targetId,element){
		var baseUrl = $("#baseUrl").val();
		var data = {type:type,action:action,targetId:targetId};
		$.ajax({    
	        type: "POST",  
	        url: baseUrl+"user/doAction.json",
	        contentType : 'application/json',
            data:JSON.stringify(data),
            dataType:'json',
	        success: function(response){ 
	        	if(response){
	        		if(response.ok){
	        			if(response.msg==="success"){
	        				var text='';
	        				if(action==="collect"){
	        					text='+1';
	        				}
	        				if(action==="cancel-collect"){
	        					$(element).parent().parent().parent().parent().slideUp();
	        				}
	        				if(action==='favor'){
	        					text='+1';
	        				}
	        				if(action==='disagree'){
	        					text='+1';
	        				}
	        				if(action==='agree'){
	        					text='+1';
	        				}
	        				$(element).tooltip({'title':text,'trigger':'manual'});
	        				$(element).tooltip("show");
	        				setTimeout(function(){$(element).tooltip("hide");$(element).tooltip("destroy");addOne(element);},1000);
	        			}
	        			if(response.msg==="exist"){
	        				var text='';
	        				if(action==="collect"){
	        					text='重复收藏';
	        				}
	        				if(action==="cancel-collect"){
	        					text="网络出错";
	        				}
	        				if(action==='favor'){
	        					text='曾经喜欢过了';
	        				}
	        				if(action==='disagree'){
	        					text='已有脚印了';
	        				}
	        				if(action==='agree'){
	        					text='已有脚印了';
	        				}
	        				$(element).tooltip({'title':text,'trigger':'manual'});
	        				$(element).tooltip("show");
	        				setTimeout(function(){$(element).tooltip("hide");},1000);
	        			}
	        			if(response.msg==="error"){
	        				$(element).tooltip({'title':'不要乱点，谢谢！','trigger':'manual'});
	        				$(element).tooltip("show");
	        				setTimeout(function(){$(element).tooltip("hide");$(element).tooltip("destroy");},1000);
	        			}
	        		}else{
	        			if(response.msg==='请先登录'){
	        				triggerLogin();
	        			}
	        		}
	        	}
	        },
		 	error: function(e){    }
		});
	}
	function doActionComment(type,action,targetId,element){
		var baseUrl = $("#baseUrl").val();
		var data = {type:type,action:action,targetId:targetId};
		$.ajax({    
	        type: "POST",  
	        url: baseUrl+"user/doCommentAction.json",
	        contentType : 'application/json',
            data:JSON.stringify(data),
            dataType:'json',
	        success: function(response){ 
	        	if(response){
	        		if(response.ok){
	        			if(response.msg==="success"){
	        				var text="+1";
	        				$(element).tooltip({'title':text,'trigger':'manual'});
	        				$(element).tooltip("show");
	        				setTimeout(function(){$(element).tooltip("hide");$(element).tooltip("destroy");addOne(element);},1000);
	        			}
	        			if(response.msg==="error"){
	        				$(element).tooltip({'title':'不要乱点，谢谢！','trigger':'manual'});
	        				$(element).tooltip("show");
	        				setTimeout(function(){$(element).tooltip("hide");$(element).tooltip("destroy");},1000);
	        			}
	        		}else{
	        			if(response.msg==='请先登录'){
	        				triggerLogin();
	        			}
	        		}
	        	}
	        },
		 	error: function(e){    }
		});
	}
	
	function addOne(obj){
		var num = $(obj).find("em").eq(0).html();
		$(obj).find("em").eq(0).html(++num);
		$(obj).removeAttr("onclick");
		$(obj).addClass("disabled");
		if($(obj).hasClass("up") || $(obj).hasClass("down")){
			$(obj).parent().find("span.action.up").removeAttr("onclick");
			$(obj).parent().find("span.action.up").addClass("disabled");
			$(obj).parent().find("span.action.down").removeAttr("onclick");
			$(obj).parent().find("span.action.down").addClass("disabled");
		}
	}
	
	function buildDate(timestamp){
		var date = new Date();
		if(date.getTime()-60*1000<=timestamp){
			return "刚刚";
		}
		if(date.getTime()-2*60*1000<=timestamp){
			return "2分钟前";
		}
		if(date.getTime()-3*60*1000<=timestamp){
			return "3分钟前";
		}
		if(date.getTime()-4*60*1000<=timestamp){
			return "4分钟前";
		}
		if(date.getTime()-5*60*1000<=timestamp){
			return "5分钟前";
		}
		if(date.getTime()-10*60*1000<=timestamp){
			return "10分钟前";
		}
		if(date.getTime()-15*60*1000<=timestamp){
			return "15分钟前";
		}
		if(date.getTime()-20*60*1000<=timestamp){
			return "20分钟前";
		}
		if(date.getTime()-25*60*1000<=timestamp){
			return "25分钟前";
		}
		if(date.getTime()-30*60*1000<=timestamp){
			return "半小时前";
		}
		if(date.getTime()-24*60*60*1000<=timestamp){
			return "今天 "+(new Date(timestamp)).Format("hh:mm:ss");
		}
		if(date.getTime()-48*60*60*1000<=timestamp){
			return "昨天 "+(new Date(timestamp)).Format("hh:mm:ss");
		}
		return (new Date(timestamp)).Format("yyyy-MM-dd hh:mm:ss");
		
	}
	Date.prototype.Format = function(fmt){ 
	  var o = {   
	    "M+" : this.getMonth()+1,                 //月份   
	    "d+" : this.getDate(),                    //日   
	    "h+" : this.getHours(),                   //小时   
	    "m+" : this.getMinutes(),                 //分   
	    "s+" : this.getSeconds(),                 //秒   
	    "q+" : Math.floor((this.getMonth()+3)/3), //季度   
	    "S"  : this.getMilliseconds()             //毫秒   
	  };   
	  if(/(y+)/.test(fmt))   
	    fmt=fmt.replace(RegExp.$1, (this.getFullYear()+"").substr(4 - RegExp.$1.length));   
	  for(var k in o)   
	    if(new RegExp("("+ k +")").test(fmt))   
	  fmt = fmt.replace(RegExp.$1, (RegExp.$1.length==1) ? (o[k]) : (("00"+ o[k]).substr((""+ o[k]).length)));   
	  return fmt;   
	}

	
	function triggerLogin(){
		var css=""; 
		css+=".minilogin .body{background:#FFFFFF;z-index:101;height:320px;width:420px;position:fixed;top:50%;left:50%;margin-left:-210px;margin-top:-160px;border: 1px solid rgba(0, 0, 0, 0.3);box-shadow: 0 3px 3px rgba(0, 0, 0, 0.3);}";
		css+=".minilogin .body .hd{display:block;height:30px;line-height:30px;padding:0 12px;background-color: #EBF2F7;border-bottom: 1px solid #D9D9D9;overflow: hidden;}";
		css+=".minilogin .body .hd h3{display:block;height:30px;line-height:30px;padding:0 6px;margin:0;}";
		css+=".minilogin .body .hd h3 p{margin:0;font-family: 'Microsoft YaHei','WenQuanYi Micro Hei',SimHei,tohoma,sans-serif;font-size:14px;}";
		css+=".minilogin .body .hd span{display:block;position:absolute;top:8px;right:8px;height:12px;width:12px;line-height:12px;text-align:center;color:#999;font-size:14px;font-weight:bold;cursor:pointer;}";
		css+=".minilogin .body .bd{height:280px;width:380px;}";
		css+=".minilogin .bg{background:#FFFFFF;position:fixed;top:0;left:0;z-index:100;width:100%;height:100%;filter:alpha(opacity=30);-moz-opacity:0.3;-khtml-opacity: 0.3;opacity: 0.3;}";
		var html = "";
		html+="<div class='box minilogin' id='minilogin'>";
		html+="<div class='bg'></div>";
		html+="<style>"+css+"</style>";
		html+="<div class='body'>";
		html+="<div class='hd'>";
		html+="<h3><p>登录</p></h3>";
		html+="<span title='关闭' onclick='closeLoginBox()'>×</span>";
		html+="</div>";
		html+="<div class='bd'>";
		html+="<iframe src='user/minilogin.html' width='380' height='280' frameborder='0'></iframe>";
		html+="</div>";
		html+="</div>";
		html+="</div>";
		
		$("body").append(html);
		
	}
	function closeLoginBox(){
		$("div#minilogin.minilogin").remove();
	}
	function reflashPage(){
		setTimeout(function(){location.reload();},100);
	}
	function takeGlance(element){
		if($(element).html().indexOf("收起")>=0){
			$(element).parent().parent().find('div.bd').eq(0).slideUp();
			$(element).html("瞥一眼");
		}else{
			$(element).parent().parent().find('div.bd').eq(0).slideDown();
			$(element).html("收起");
		}
	}
	